<template>
    <el-form
        :model="ruleForm2"
        :rules="rules2"
        ref="ruleForm2"
        label-position="left"
        label-width="0px"
        class="demo-ruleForm login-container"
    >
        <h3 class="title">车智洗系统登录</h3>
        <el-form-item prop="account">
            <el-input type="text" v-model="ruleForm2.account" clearable placeholder="账号"></el-input>
        </el-form-item>
        <el-form-item prop="checkPass">
            <div class="verify">
                <el-input
                    style="width:250px;"
                    type="password"
                    show-password
                    @keyup.enter.native="loginClick(ruleForm2)"
                    v-model="ruleForm2.checkPass"
                    placeholder="密码"
                ></el-input>
                <el-button type="primary" @click="verification">验证</el-button>
            </div>
        </el-form-item>

        <div class="password-flex">
            <el-checkbox v-model="checked" checked class="remember">记住密码</el-checkbox>
            <!-- <div class="forget" @click="forgetPassword">忘记密码?</div> -->
        </div>
        <el-form-item style="width:100%;">
            <el-button
                type="primary"
                style="width:100%;"
                @click.native="loginClick(ruleForm2)"
                :loading="logining"
            >登录</el-button>
        </el-form-item>
    </el-form>
</template>

<script>
//   import { requestLogin } from '../api/api';
import login from "../../util/login.js";
import utils from "../../util/request";
export default {
    data() {
        return {
            isSuccess: false, //验证是否成功，默认不成功
            logining: false,
            ruleForm2: {
                account: "",
                checkPass: ""
            },
            rules2: {
                account: [
                    { required: true, message: "请输入账号", trigger: "blur" }
                ],
                checkPass: [
                    { required: true, message: "请输入密码", trigger: "blur" }
                ]
            },
            checked: true
        };
    },
    methods: {
        verification() {
            let that = this;
            let Base64 = require("js-base64").Base64;
            let captcha = new TencentCaptcha(
                "2074717047",
                rsp => {
                    // console.log(rsp);
                    if (rsp.ret === 0) {
                        //当ret是0得时候代表成功填写了验证码，我在里面进行了axios操作。
                        this.isSuccess = true;
                        that.$refs.ruleForm2.validate(valid => {
                            if (valid) {
                                that.logining = true;
                                let data = {page:"1",list_rows:"10",phone:that.ruleForm2.account,password:that.ruleForm2.checkPass}
                                utils.request("/person/list","post",JSON.stringify(data)).then(resp=>{
                                    console.log(resp)
                                    if(resp.data.code === 200 && resp.data.data.data.length > 0){
                                        data.id = resp.data.data.data[0].id
                                        sessionStorage.setItem("userdata",Base64.encode(JSON.stringify(resp.data.data.data[0])))
                                        // setTimeout(()=>{
                                            that.$router.push("/home")
                                            // sessionStorage.setItem("userdata",Base64.encode(JSON.stringify(data)))
                                        // },300)
                                    }else{
                                        that.logining = false;
                                        that.$message.error("账号或密码错误，请核对后重新登录")
                                    }
                                })
                                // var loginParams = { username: that.ruleForm2.account, password: that.ruleForm2.checkPass };
                                // login.login(
                                //         "authentication/login?username=" +
                                //             that.ruleForm2.account +
                                //             "&password=" +
                                //             that.ruleForm2.checkPass,
                                //         "post"
                                //     )
                                //     .then(res => {
                                //         that.logining = false;
                                //         console.log(res);
                                //         if (res.data.code == 20000) {
                                //             if (that.checked == true) {
                                //                 localStorage.che_userPass = Base64.encode(
                                //                     that.ruleForm2.checkPass
                                //                 );
                                //             }
                                //             let data = res.data.data;
                                //             sessionStorage.che_roleId =
                                //                 5; //权限
                                //             sessionStorage.che_token = Base64.encode(
                                //                 data.token
                                //             );
                                //             sessionStorage.che_userId =
                                //                 data.userId;
                                //             localStorage.che_userName = Base64.encode(
                                //                 that.ruleForm2.account
                                //             );
                                //             that.$message({
                                //                 message: "登录成功",
                                //                 type: "success"
                                //             });
                                //             that.$router.push("/home");
                                //             that.logining = false;
                                //             that.isSuccess = false;
                                //         } else if (res.data.code == 20002) {
                                //             that.$message({
                                //                 message: "账号或密码有误！",
                                //                 type: "error"
                                //             });
                                //             that.logining = false;
                                //             that.isSuccess = false;
                                //         }
                                //     });
                            } else {
                                console.log("error submit!!");
                                return false;
                            }
                        });
                    } else {
                        this.isSuccess = false;
                        this.$message({
                            message: "验证不通过",
                            type: "warning"
                        });
                    }
                },
                {}
            );
            captcha.show();
        },
        forgetPassword() {
            this.$router.push({ path: "/forgetPassword" });
        },
        loginClick() {
            let that = this;
            // let Base64 = require("js-base64").Base64;
            // if (that.isSuccess === true) {
            //     that.$refs.ruleForm2.validate(valid => {
            //         if (valid) {
            //             that.logining = true;
            //             // var loginParams = { username: that.ruleForm2.account, password: that.ruleForm2.checkPass };
            //             login
            //                 .login(
            //                     "authentication/login?username=" +
            //                         that.ruleForm2.account +
            //                         "&password=" +
            //                         that.ruleForm2.checkPass,
            //                     "post"
            //                 )
            //                 .then(res => {
            //                     that.logining = false;
            //                     console.log(res);
            //                     if (res.data.code == 20000) {
            //                         if (that.checked == true) {
            //                             localStorage.che_userPass = Base64.encode(
            //                                 that.ruleForm2.checkPass
            //                             );
            //                         }
            //                         let data = res.data.data;
            //                         sessionStorage.che_role = data.role; //权限
            //                         sessionStorage.che_token = Base64.encode(
            //                             data.token
            //                         );
            //                         sessionStorage.che_userId = data.userId;
            //                         localStorage.che_userName = Base64.encode(
            //                             that.ruleForm2.account
            //                         );
            //                         that.$message({
            //                             message: "登录成功",
            //                             type: "success"
            //                         });
            //                         that.$router.push("/home");
            //                         that.logining = false;
            //                         that.isSuccess = false;
            //                     } else if (res.data.code == 20002) {
            //                         that.$message({
            //                             message: "账号或密码有误！",
            //                             type: "error"
            //                         });
            //                         that.logining = false;
            //                         that.isSuccess = false;
            //                     }
            //                 });
            //         } else {
            //             console.log("error submit!!");
            //             return false;
            //         }
            //     });
            // } else {
            that.verification();
            // }
        }
    },
    created() {
        this.userdata = sessionStorage.getItem("userdata")
        if(this.userdata !== null){
            this.$router.push("/home")
        }else{return false}
        // let Base64 = require("js-base64").Base64;
        // let Base64 = require('js-base64').Base64
        // this.ruleForm2.account = Base64.decode(localStorage.che_userName);
        // this.ruleForm2.checkPass = Base64.decode(localStorage.che_userPass);
    },
    mounted() {
        // this.callback();
    }
};
</script>

<style lang="scss" scoped>
.login-container {
    /*box-shadow: 0 0px 8px 0 rgba(0, 0, 0, 0.06), 0 1px 0px 0 rgba(0, 0, 0, 0.02);*/
    -webkit-border-radius: 5px;
    border-radius: 5px;
    -moz-border-radius: 5px;
    background-clip: padding-box;
    margin: 180px auto;
    width: 350px;
    padding: 35px 35px 15px 35px;
    background: #fff;
    border: 1px solid #eaeaea;
    box-shadow: 0 0 25px #cac6c6;
    .title {
        margin: 0px auto 40px auto;
        text-align: center;
        color: #505458;
    }
    .verify {
        display: flex;
        align-items: center;
        justify-content: space-between;
    }
    .password-flex {
        display: flex;
        align-items: center;
        justify-content: space-between;
        .forget {
            margin-bottom: 20px;
            font-size: 14px;
            color: #409eff;
            cursor: pointer;
        }
    }
    .remember {
        margin: 0px 0px 20px 0px;
    }
}
</style>